package com.neuedu.controller;

import com.neuedu.common.Const;
import com.neuedu.common.JSONData;
import com.neuedu.pojo.Shipping;
import com.neuedu.pojo.UserInfo;
import com.neuedu.service.ShippingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/shipping/")
@Api(value = "收货地址相关接口",description = "收货地址相关接口")
public class ShippingController {
    @Autowired
    private ShippingService shippingService;

    @ApiOperation(value = "收货地址的新增",notes = "收货地址的新增",httpMethod = "POST")
    @RequestMapping(value = "addShipping",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> addShipping(HttpSession session, Shipping shipping){
        //-判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("未登录，请先登录");
        }
        //-service
        JSONData<String> data = shippingService.addShipping(userInfo.getId(), shipping);
        return data;
    }
    @ApiOperation(value = "收货地址的修改",notes = "收货地址的修改",httpMethod = "POST")
    @RequestMapping(value = "updateShippingByIdAndUserId",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> updateShippingByIdAndUserId(HttpSession session,Shipping shipping){
        //-判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("未登录，请先登录");
        }
        JSONData<String> data = shippingService.updateShippingByIdAndUserId(userInfo.getId(), shipping);
        return data;
    }
    //-查看某个收货地址的详情：id、userid
    @ApiOperation(value = "根据ID查询收货地址",notes = "根据ID查询收货地址",httpMethod = "POST")
    @RequestMapping(value = "selShippingByIdAndUserId",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<Shipping> selShippingByIdAndUserId(HttpSession session,Integer shippingId){
        //-判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("未登录，请先登录");
        }
        JSONData<Shipping> data = shippingService.selShippingByIdAndUserId(userInfo.getId(), shippingId);
        return data;
    }
    @ApiOperation(value = "查询所有收货地址by用户",notes = "查询所有收货地址by用户",httpMethod = "POST")
    @RequestMapping(value = "selAllShippingByUserId",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<List<Shipping>> selAllShippingByUserId(HttpSession session){
        //-判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("未登录，请先登录");
        }
        JSONData<List<Shipping>> data = shippingService.selAllShippingByUserId(userInfo.getId());
        return data;
    }
    //-收货地址删除：考虑批量删除：通过 userid--shippingid
    //-收货地址是多个：可以和前端 约定：shippingId,shippingId,。。。
    @ApiOperation(value = "删除收货地址",notes = "删除收货地址",httpMethod = "POST")
    @RequestMapping(value = "delShippingByShippingIds",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<List<Shipping>> delShippingByShippingIds(HttpSession session,String shippingIds){
        //-判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("未登录，请先登录");
        }
        JSONData<List<Shipping>> data = shippingService.delShippingByShippingIds(userInfo.getId(), shippingIds);
        return data;
    }
}
